
package JoseH;
import java.sql.*;



public class Chofer {
    
    Conectate con;
  
    public Chofer (){
    con = new Conectate();
    }
    
    
    public void nuevaPersona(String rut, String nombre, String ap, String mov, String sex){
        try {            
            PreparedStatement pstm = con.getConnection().prepareStatement("insert into " + 
                    "CHOFER(RUT_CHOFER, NOMBRE_CHOFER, APELLIDO_CHOFER, MOVIL_CHOFER, SEXO_CHOFER) " + " values(?,?,?,?,?)");            
            pstm.setString(1, rut);
            pstm.setString(2, nombre);
            pstm.setString(3, ap);                        
            pstm.setString(4, mov);  
            pstm.setString(5, sex);
            pstm.execute();
            pstm.close();            
            }
        
        catch(SQLException e){
            System.out.println(e);
            }
        }
    
   
    public void deletePersona(String cod){  
        try {                
            PreparedStatement pstm = con.getConnection().prepareStatement("delete from CHOFER where RUT_CHOFER = ?");            
                pstm.setString(1, cod);                   
                pstm.execute();
                pstm.close();            
            }
        
        catch(SQLException e){
            System.out.println(e);
            }            
        }
     public void updatePersona(String rut, String nombre, String ap, String mov, String sex ){
        try {            
           PreparedStatement pstm = con.getConnection().prepareStatement("update CHOFER " +
            "set NOMBRE_CHOFER  = ? , " +                    
            "APELLIDO_CHOFER = ? ," +
            "MOVIL_CHOFER = ? ," +
            "SEXO_CHOFER = ? " +
            "where RUT_CHOFER = ? ");  
            pstm.setString(1, nombre);
            pstm.setString(2, ap);
            pstm.setString(3, mov);
            pstm.setString(4, sex);
            pstm.setString(5, rut);
            pstm.execute();
            pstm.close();            
      }catch(SQLException e){
            System.out.println(e);
      }
     }
    
    

 
public Object [][] getDatos(){
    
        int registros = 0;      
     
        try {         
            PreparedStatement pstm = con.getConnection().prepareStatement("SELECT count(1) as total FROM CHOFER ");
            ResultSet res = pstm.executeQuery();
            res.next();
            registros = res.getInt("total");
            res.close();
            }
        
        catch(SQLException e){
            System.out.println(e);
        }
      
    Object[][] data = new String[registros][5];  

        try {    
            PreparedStatement pstm = con.getConnection().prepareStatement("SELECT " +
            " RUT_CHOFER, NOMBRE_CHOFER, APELLIDO_CHOFER, MOVIL_CHOFER, SEXO_CHOFER " +
            " FROM CHOFER" +
            " ORDER BY RUT_CHOFER ");
            ResultSet res = pstm.executeQuery();
        
        int i = 0;
            while(res.next()){
            String estRut = res.getString("RUT_CHOFER");
            String estNombre = res.getString("NOMBRE_CHOFER");
            String estApellido = res.getString("APELLIDO_CHOFER");
            String estMovil = res.getString("MOVIL_CHOFER");
            String estSexo = res.getString("SEXO_CHOFER");
            data[i][0] = estRut;            
            data[i][1] = estNombre;            
            data[i][2] = estApellido;            
            data[i][3] = estMovil;            
            data[i][4] = estSexo;            
            i++;
            }
            
        res.close();
        }
        
        catch(SQLException e){
            System.out.println(e);
        }
    
        return data;
        
    }
}
